package com.hcloud.v1.admin.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.hcloud.v1.admin.entity.JsonResult;
import com.hcloud.v1.admin.entity.User;
import com.hcloud.v1.admin.service.AccountService;
import com.hcloud.v1.jwt.entity.AccessToken;
import com.hcloud.v1.jwt.entity.JwtProperty;
import com.hcloud.v1.jwt.entity.ResultStatusCode;
import com.hcloud.v1.jwt.util.jwt.JwtHelper;
@RestController
public class AccountController {

	@Autowired
	private AccountService accountService;

	private Gson gson = new Gson();

	private JwtProperty jwtProperty = new JwtProperty();

	/**
	 * 登录
	 * 
	 * <pre>
	 * 登录
	 * </pre>
	 * 
	 * @author lisheng
	 * @param accountName
	 * @param accountPwd
	 * @param clientId
	 * @return
	 */
	// @ApiOperation(value = "使用JWT进行token验证，传入登录参数")
	@RequestMapping(value = "/oauth/token", method = RequestMethod.POST)
	public String getAccessToken(String username, String password, String clientId) throws Exception {
		JsonResult result = new JsonResult();

		try {
			if (clientId == null || (clientId.compareTo(jwtProperty.getClientId()) != 0)) {

				result.setStatus(ResultStatusCode.INVALID_CLIENTID.getErrorCode());
				result.setMsg(ResultStatusCode.INVALID_CLIENTID.getErrorMsg());

				return gson.toJson(result);
			}

			User user = null;
			try {
				user = accountService.login(username, password);
			} catch (Exception e) {
				return new JsonResult(null, e.getMessage()).failure().toString();
			}

			// 拼装accessToken
			String accessToken = JwtHelper.createJWT(username, user.getId().toString(), "userRole", jwtProperty.getClientId(),
					jwtProperty.getName(), jwtProperty.getExpiresSecond() * 1000, jwtProperty.getBase64Secret());

			// 返回accessToken
			AccessToken accessTokenEntity = new AccessToken();
			accessTokenEntity.setAccessToken("bearer " + accessToken);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("accessToken", "bearer " + accessToken);
			map.put("userId", user.getId());
			map.put("userName", user.getUsername());
			map.put("name", user.getName());
			map.put("mobilePhone", user.getMobilePhone());
			map.put("telPhone", user.getTelPhone());
//			map.put("avatar", user.getAvatar()); // 头像
//			map.put("deptId", dept.getId());
//			map.put("deptName", dept.getName());

			// 如果是后台管理登录的话，则继续查询一下这个人的所有的权限，所有的操作授权数据
//			if (logonMode == 1) {
//				List<Authorization> lst = authorizationService.getAuthorizationsByUserId(user.getId());
//				List perms = new ArrayList<>();
//
//				for (Authorization object : lst) {
//					perms.add(object.getPageInnerName() + ":" + object.getElementInnerName());
//				}
//				map.put("perms", perms);
//			}

			result.setStatus(ResultStatusCode.OK.getErrorCode());
			result.setMsg(ResultStatusCode.OK.getErrorMsg());
			result.setData(map);

			return gson.toJson(result);
		} catch (Exception ex) {
			result.setStatus(ResultStatusCode.SYSTEM_ERR.getErrorCode());
			result.setMsg(ResultStatusCode.SYSTEM_ERR.getErrorMsg());
			ex.printStackTrace();
			return gson.toJson(result);
		}
	}
}